import { targetWeightModel } from "@/models/targetWeight"
import { USER } from '@/services/admin/typings'
import { getUserListByRoleKey } from '@/services/admin/user'
import { getTargetBySeller,TargetBySellerType } from '@/services/StatisticsService'
import { getCustomerChannelTarget } from '@/services/TargetService'
import dayjs from 'dayjs'
import { flow,makeAutoObservable,runInAction } from 'mobx'
import { when } from "mobx/src/api/when"

class TargetModel {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true })
  }

  drawerState: boolean = true
  setDrawerState(state: boolean) {
    this.drawerState = state
  }

  openDrawerState() {
    this.fetchSellerList()
    this.fetchOperationList()
    // this.fetchCustomerChannelTarget()
    this.setDrawerState(true)
  }

  closeDrawerState() {
    this.setDrawerState(false)
  }

  targetStatisticsData?: TargetBySellerType[]

  async flushTargetStatisticsData(month: dayjs.Dayjs) {
    const targetBySeller = await getTargetBySeller({ endDay: month, startDay: month })
    runInAction(() => {
      this.targetStatisticsData = targetBySeller
    })
  }

  get allMoneyTarget() {
    return (
      this.targetStatisticsData?.filter((item) => item.type === '1').reduce((acc, curr) => acc + curr.target, 0) || 0
    )
  }

  get allMoneyCurrValue() {
    return (
      this.targetStatisticsData?.filter((item) => item.type === '1').reduce((acc, curr) => acc + curr.value, 0) || 0
    )
  }

  get allCustomerTarget() {
    return (
      this.targetStatisticsData?.filter((item) => item.type === '2').reduce((acc, curr) => acc + curr.target, 0) || 0
    )
  }

  get allCustomerCurrValue() {
    return (
      this.targetStatisticsData?.filter((item) => item.type === '2').reduce((acc, curr) => acc + curr.target, 0) || 0
    )
  }

  get allCustomerValueTarget() {
    return (
      this.targetStatisticsData?.filter((item) => item.type === '2').reduce((acc, curr) => acc + curr.value, 0) || 0
    )
  }

  // 员工列表
  sellerList?: USER.FetchUserPage
  operationList?: USER.FetchUserPage

  /**
   * 获取销售人员列表
   */
  fetchSellerList = flow(
    function* () {
      // @ts-ignore
      this.operationList = yield getUserListByRoleKey('seller')
    }.bind(this),
  )

  /**
   * 获取运营人员列表
   */
  fetchOperationList = flow(
    function* () {
      // @ts-ignore
      this.sellerList = yield getUserListByRoleKey('operation')
    }.bind(this),
  )

  // 渠道目标数据
  customerChannelTarget: any[] = [
    { id: 1, name: '抖音', target: 500, rating: 30 },
    { id: 2, name: '小红书', target: 500, rating: 30 },
    { id: 3, name: '微信', target: 500, rating: 40 },
  ]

  get allRating() {
    return this.customerChannelTarget.reduce((acc, curr) => acc + curr.rating, 0)
  }

  fetchCustomerChannelTarget = flow(
    function* () {
      // @ts-ignore
      this.customerChannelTarget = yield getCustomerChannelTarget()
    }.bind(this),
  )

  updateTarget() {

  }
}



export default TargetModel
export const targetModel = new TargetModel()